package com.liang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liang.mall.entity.Role;
import com.liang.mapper.RoleMapper;
import com.liang.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liang
 * @since 2021-05-12
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    RoleMapper roleMapper;





    @Override
    public IPage<Role> getRoles(Integer pageNum, Integer pageSize, String roleName) {
        return  roleMapper.selectPage(new Page<Role>(pageNum,pageSize),new QueryWrapper<Role>().like("remark",roleName));
    }

    @Override
    @Transactional
    public boolean removeRoleById(Integer id) {

        //删除 用户 角色中间表的数据
        Integer integer = roleMapper.deleteUser_Role_ByRoleId(id);


        //删除 角色资源表的数据
        Integer integer1 = roleMapper.delete_Role_Resource_ByRoleId(id);

        //删除 角色表的数据
        Integer integer2 = roleMapper.delete_Role_ByRoleId(id);

        return integer2>0;
    }

    @Override
    @Transactional
    public void addRoleResource(Integer id, ArrayList<Integer> menuIdList) {

//        添加角色中间表的数据

        for (Integer menuId : menuIdList) {

            roleMapper.insertRoleResource(id,menuId);

        }




    }

//    @Override
//    public Boolean AddRole(Role role, ArrayList<Integer> menuIdList) {
//
//        int insert = roleMapper.insert(role);
//
//        roleMapper.insertRoleResouce(menuIdList);
//
//
//    }
}
